﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenSSL.Core;
using OpenSSL.Crypto;
using Sicurezza.Entita;

namespace Sicurezza.Client.Manager
{
    public class KeyManager
    {
        public static CryptoKey Load(int serial, string password)
        {
            string key = FileManager.LoadPrivateKey(serial);
            CryptoKey k = null;
            try
            {
                k = CryptoKey.FromPrivateKey(new BIO(key), password);
            }
            catch (OpenSslException ex)
            {
                throw new ManagerException("Incorrect password. Try again");
            }
            return k;
        }

        public static RSA Generate(int bit)
        {
            RSA key = new RSA();
            key.GenerateKeys(bit, 17, null, null);
            return key;
        }




        public static byte[] Generate()
        {
            byte[] key = new byte[64];
            Guid.NewGuid().ToByteArray().CopyTo(key, 0);
            Guid.NewGuid().ToByteArray().CopyTo(key, 16);
            Guid.NewGuid().ToByteArray().CopyTo(key, 32);
            Guid.NewGuid().ToByteArray().CopyTo(key, 48);
            return key;
        }

        public static byte[] GenerateIV()
        {
            return Guid.NewGuid().ToByteArray();
        }
    }
}
